package com.jz.community.modulemine.information.ui;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.bean.BaseResponseText;
import com.jz.community.basecomm.bean.LoginInfo;
import com.jz.community.basecomm.bean.UserInfo;
import com.jz.community.basecomm.task.GetCouponModeTask;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.utils.BaseSpUtils;
import com.jz.community.basecomm.utils.CommUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.StringCommUtils;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.modulemine.R;
import com.jz.community.modulemine.R2;
import com.jz.community.modulemine.information.task.UpdateUserInfoTask;
import com.jz.community.modulemine.presenter.BindingPhonePresenter;
import com.jz.community.modulemine.ui.BindingPhoneView;
import com.jz.community.modulemine.ui.view.TimeCount;

import butterknife.BindView;

/**
 * Created by Admin on 2018/3/30 0030.
 */

public class BindingPhoneActivity extends BaseMvpActivity<BindingPhoneView.View, BindingPhonePresenter> implements BindingPhoneView.View {


    @BindView(R2.id.title_back)
    ImageButton titleBack;
    @BindView(R2.id.title_name)
    TextView titleName;
    @BindView(R2.id.edit_input_phone)
    EditText editInputPhone;
    @BindView(R2.id.text_code)
    TextView textCode;
    @BindView(R2.id.edit_input_code)
    EditText editInputCode;
    @BindView(R2.id.button_submit)
    Button buttonSubmit;
    @BindView(R2.id.title_toolbar)
    Toolbar titleToolbar;

    TimeCount timeCount;
    //第三方绑定
    public static final String SMS_TYPE_REGISTER = "2";

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected int getContentView() {
        return R.layout.module_mine_activity_binding_phone;
    }

    @Override
    protected BindingPhonePresenter createPresenter() {
        return new BindingPhonePresenter(this);
    }


    @Override
    protected void initView() {
        setImmersionBar(titleToolbar);
        titleName.setText("绑定手机");
    }

    @Override
    protected void addListener() {
        titleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                CommUtils.hideKeyboard(v, BindingPhoneActivity.this);
            }
        });
        textCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringCommUtils.isLoginPhone(BindingPhoneActivity.this, editInputPhone.getText().toString().trim())) {
                    mPresenter.getSmsCode(editInputPhone.getText().toString().trim(), SMS_TYPE_REGISTER);
                }
            }
        });
        buttonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tel = editInputPhone.getText().toString().trim();
                String code = editInputCode.getText().toString().trim();
                if (isHasEmptyDta(tel, code)) {
                    getBindPhoneTask(tel);
                }
            }
        });
    }

    private void getBindPhoneTask(String tel) {
        new UpdateUserInfoTask(this, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                UserInfo userInfo = (UserInfo) o;
                if (!Preconditions.isNullOrEmpty(userInfo.getPhone())) {
                    WpToast.l(BindingPhoneActivity.this, "绑定成功");
                    handleCouponModel();
                    Intent intent = new Intent();
                    intent.putExtra("phone", userInfo.getPhone());
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    WpToast.l(BindingPhoneActivity.this, "绑定失败");
                }

            }
        }, true).execute(tel, "2");
    }

    /**
     * 绑定手机发放优惠券
     */
    private void handleCouponModel() {
        new GetCouponModeTask(this, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {

            }
        }).execute("4");
    }

    @Override
    public void showSmsCode(LoginInfo baseResponseInfo) {
        if (!Preconditions.isNullOrEmpty(baseResponseInfo.getVcode())) {
            BaseSpUtils.getInstance().setSmsCode(this, baseResponseInfo.getVcode());
            WpToast.l(this, "验证码发送成功");
            if (timeCount == null) {
                timeCount = new TimeCount(120 * 1000, 1000, BindingPhoneActivity.this, textCode);
            }
            timeCount.start();
        } else {
            WpToast.l(this, baseResponseInfo.getMessage());
        }
    }


    @Override
    public void showCheckWifiRegister(BaseResponseText responseText) {
        if (responseText.getCode() == 0) {
            BaseSpUtils.getInstance().getUserBaseInfo(this).setName(editInputPhone.getText().toString().trim());
            ConnectWifiReceiver();
            setResult(1);
            finish();
        }
    }

    private void ConnectWifiReceiver() {
        Intent intentToConnectWifi = new Intent();
        intentToConnectWifi.putExtra("mobile", editInputPhone.getText().toString().trim());
        intentToConnectWifi.setAction("toConnectWifi");
        sendBroadcast(intentToConnectWifi);
    }

    public boolean isHasEmptyDta(String tel, String code) {
        if (Preconditions.isNullOrEmpty(tel)) {
            WpToast.l(this, "手机号不能为空");
            return false;
        }
        if (!StringCommUtils.isPhone(tel)) {
            WpToast.l(this, "请输入有效的手机号");
            return false;
        }
        if (Preconditions.isNullOrEmpty(code) || !code.equals(BaseSpUtils.getInstance().getSmsCode(this))) {
            WpToast.l(this, "请输入正确的验证码");
            return false;
        }
        return true;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timeCount != null) {
            timeCount.cancel();
        }
    }
}
